#include "Bsp_uart.h"

#define BUF1_LEN 100

static uint8_t RxProBuf1[BUF1_LEN];//串口1接收缓存区


/*使用DMA传输 务必使用静态数组当缓存*/
/*重启串口接收 务必保证该函数在串口中断中调用*/
void Uart_RxAgain1(void)
{
//    HAL_UARTEx_ReceiveToIdle_IT(huart, RxProBuf, PRO_BUF_LEN1);//使用中断
    HAL_UARTEx_ReceiveToIdle_DMA(&huart1, RxProBuf1, BUF1_LEN);//使用DMA
    __HAL_DMA_DISABLE_IT(huart1.hdmarx, DMA_IT_HT); /* 关闭DMA传输过半中断，避免多次进入中断 */
}

//DMA串口接收回调
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
{
    
	if(huart->Instance == USART1)
    {
        BSP_uart_TX(&huart1,RxProBuf1,Size);
    }
}

//串口启动
void BSP_uart_start(void)
{ 
    __HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);    //启用串口闲时中断

    Uart_RxAgain1();        //启动串口接收
}

//通过串口发送
void BSP_uart_TX(UART_HandleTypeDef *huart, uint8_t *buffer_out, uint16_t buf_size)
{
	HAL_UART_Transmit_DMA(huart, (uint8_t*)buffer_out, buf_size);
}

/*C语言printf，gets函数支持，需勾选keil微库*/
#include <stdio.h>
int fputc(int ch, FILE *f){
	HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 2);
	return ch;
}
int fgetc(FILE *f){
	uint8_t ch = 0;
	HAL_UART_Receive(&huart1, &ch, 1, 2);
	return ch;
}

